export const pagingMixin = {
  data() {
    return {
      page: 1, // 当前第几页
      pageSize: 10, // 每页多少条数据
      total: 0, // 总条数
      list: [], // 列表数据
      isEmpty: false, // 是否为空
    }
  },
  mounted() {
    this.getPagingList(true)
  },
  methods: {
    // 直接请求某一页的数据
    pageChange(page) {
      this.page = page;
      this.getPagingList(true);
    },
    // 分页接口返回数据处理
    resHandlerInMixin(res, mapFn = v => v, isRefresh = false) {
      uni.stopPullDownRefresh();
      const { lists, total = 0 } = res;
      const handledList = (lists || []).map(mapFn);
      if (isRefresh) {
        this.list = handledList;
      } else {
        this.list = this.list.concat(handledList);
      }
      this.total = total;
      this.isEmpty = this.list.length === 0;
    },
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.page = 1;
    this.getPagingList(true)
  },
  // 下拉加载
  onReachBottom() {
    if (this.list.length < this.total) {
      this.page++;
      this.getPagingList();
    }
  },
}
